package com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.date.DateUtils;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeExcelVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypePageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeRespVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeTreeRespVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeTreeRespVO1;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.type.vo.TextbookTypeUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.textbookinfo.type.TextbookTypeConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.textbooktype.TextbookTypeDO;
import com.nanhai.buddhist.college.module.collegebase.service.textbookinfo.type.TextbookTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 教材分类")
@RestController
@RequestMapping("/collegebase/textbook-type")
@Validated
public class TextbookTypeController {

    @Resource
    private TextbookTypeService textbookTypeService;

    @PostMapping("/create")
    @ApiOperation("创建教材分类")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:create')")
    public CommonResult<String> createTextbookType(@Valid @RequestBody TextbookTypeCreateReqVO createReqVO) {
        return success(textbookTypeService.createTextbookType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教材分类")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:update')")
    public CommonResult<Boolean> updateTextbookType(@Valid @RequestBody TextbookTypeUpdateReqVO updateReqVO) {
        textbookTypeService.updateTextbookType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教材分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:delete')")
    public CommonResult<Boolean> deleteTextbookType(@RequestParam("id") String id) {
        textbookTypeService.deleteTextbookType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教材分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:query')")
    public CommonResult<TextbookTypeRespVO> getTextbookType(@RequestParam("id") String id) {
        TextbookTypeDO textbookType = textbookTypeService.getTextbookType(id);
        return success(TextbookTypeConvert.INSTANCE.convert(textbookType));
    }

    @GetMapping("/list")
    @ApiOperation("获得教材分类列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:query')")
    public CommonResult<List<TextbookTypeRespVO>> getTextbookTypeList(@RequestParam("ids") Collection<String> ids) {
        List<TextbookTypeDO> list = textbookTypeService.getTextbookTypeList(ids);
        return success(TextbookTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得教材分类分页")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:query')")
    public CommonResult<PageResult<TextbookTypeRespVO>> getTextbookTypePage(@Valid TextbookTypePageReqVO pageVO) {

        if (pageVO.getCreateTime() != null && pageVO.getCreateTime().length > 1) {
            Date endDateTime = pageVO.getCreateTime()[1];
            pageVO.getCreateTime()[1] = DateUtils.addDate(endDateTime, Calendar.DATE, 1);
        }
        // System.out.println("0------------------------");
        PageResult<TextbookTypeDO> pageResult = textbookTypeService.getTextbookTypePage(pageVO);
        return success(TextbookTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教材分类 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-type:export')")
    @OperateLog(type = EXPORT)
    public void exportTextbookTypeExcel(@Valid TextbookTypeExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {

        if (exportReqVO.getCreateTime() != null && exportReqVO.getCreateTime().length > 1) {
            Date endDateTime = exportReqVO.getCreateTime()[1];
            exportReqVO.getCreateTime()[1] = DateUtils.addDate(endDateTime, Calendar.DATE, 1);
        }
        List<TextbookTypeDO> list = textbookTypeService.getTextbookTypeList(exportReqVO);
        // 导出 Excel
        List<TextbookTypeExcelVO> datas = TextbookTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "教材分类.xls", "数据", TextbookTypeExcelVO.class, datas);
    }

    @GetMapping("/tree/list")
    @ApiOperation("教材分类树状图")
    public CommonResult<List<TextbookTypeTreeRespVO1>> getTextbookTypeTreeList() {

        return success(textbookTypeService.getTextbookTypeTreeList());
    }

    @GetMapping("/tree/page")
    @ApiOperation("教材分类树状图 (分页)")
    public CommonResult<PageResult<TextbookTypeTreeRespVO>> getTextbookTypeTreeList(@Valid TextbookTypePageReqVO pageVO) {
        return success(textbookTypeService.getTextbookTypeTreePage(pageVO));
    }

}
